package com.rex.saas.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rex.saas.bean.request.LabelAddRequest;
import com.rex.saas.bean.response.CustomLabelResponse;
import com.rex.saas.bean.response.LabelResponse;
import com.rex.saas.db.entity.SaasDeviceLabel;
import com.rex.saas.db.entity.SaasDeviceLabelRelation;
import com.rex.saas.db.entity.SaasSpaceLabel;
import com.rex.saas.db.mapper.SaasDeviceLabelMapper;
import com.rex.saas.db.mapper.SaasDeviceLabelRelationMapper;
import com.rex.saas.db.mapper.SaasSpaceLabelMapper;
import com.rex.saas.enums.LabelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LabelService
 * @Description:
 * @Author: zhusiyu
 */
@Component
@Slf4j
public class LabelService {
    @Autowired
    private SaasSpaceLabelMapper saasSpaceLabelMapper;

    @Autowired
    private SaasDeviceLabelMapper saasDeviceLabelMapper;

    @Autowired
    private SaasDeviceLabelRelationMapper saasDeviceLabelRelationMapper;
    public SaasDeviceLabel queryDefaultDeviceLabelByName(String labelName, Long spaceLabelId) {
        LambdaQueryWrapper<SaasDeviceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasDeviceLabel::getSpaceLabelId, spaceLabelId);
        queryWrapper.eq(SaasDeviceLabel::getLabelName, labelName);
        List<SaasDeviceLabel> list = saasDeviceLabelMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
    public SaasDeviceLabel queryDeviceLabelByName(String labelName, String customerId) {
        LambdaQueryWrapper<SaasDeviceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasDeviceLabel::getCustomerId, customerId);
        queryWrapper.eq(SaasDeviceLabel::getLabelName, labelName);
        List<SaasDeviceLabel> list = saasDeviceLabelMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public SaasSpaceLabel querySpaceLabelByName(String labelName, String customerId) {
        LambdaQueryWrapper<SaasSpaceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceLabel::getLabelName, labelName);
        if(!StringUtils.isEmpty(customerId)){
            queryWrapper.eq(SaasSpaceLabel::getCustomerId, customerId);
        }else {
            queryWrapper.eq(SaasSpaceLabel::getType, 0);
        }
        List<SaasSpaceLabel> list = saasSpaceLabelMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public boolean addDeviceLabel(LabelAddRequest labelAddRequest, String customerId) {
        SaasDeviceLabel saasDeviceLabel = new SaasDeviceLabel();
        saasDeviceLabel.setLabelName(labelAddRequest.getLabelName());
        saasDeviceLabel.setIconUrl(labelAddRequest.getIconUrl());
        saasDeviceLabel.setCustomerId(customerId);
        saasDeviceLabel.setSpaceLabelId(labelAddRequest.getSpaceLabelId());
        return saasDeviceLabelMapper.insert(saasDeviceLabel) == 1;
    }

    public boolean addSpaceLabel(LabelAddRequest labelAddRequest, String customerId) {
        SaasSpaceLabel saasSpaceLabel = new SaasSpaceLabel();
        saasSpaceLabel.setLabelName(labelAddRequest.getLabelName());
        saasSpaceLabel.setIconUrl(labelAddRequest.getIconUrl());
        if(StringUtils.isEmpty(customerId)){
            saasSpaceLabel.setType(0);
        }else {
            saasSpaceLabel.setType(1);
            saasSpaceLabel.setCustomerId(customerId);
        }
        return saasSpaceLabelMapper.insert(saasSpaceLabel) == 1;
    }

    public SaasSpaceLabel getSpaceLabelById(Long id) {
        return saasSpaceLabelMapper.selectById(id);
    }

    public boolean deleteSpaceLabel(Long id) {
        return saasSpaceLabelMapper.deleteById(id) == 1;
    }

    public SaasDeviceLabel getDeviceLabelById(Long id) {
        return saasDeviceLabelMapper.selectById(id);
    }

    public List<SaasDeviceLabelRelation> getDevicesBySpaceLabelId(Long id) {
        LambdaQueryWrapper<SaasDeviceLabelRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasDeviceLabelRelation::getSpaceLabelId, id);
        return saasDeviceLabelRelationMapper.selectList(queryWrapper);
    }

    public boolean deleteDeviceLabel(Long id) {
        return saasDeviceLabelMapper.deleteById(id) == 1;
    }

    public List<SaasDeviceLabelRelation> getDevicesByLabelId(Long id) {
        LambdaQueryWrapper<SaasDeviceLabelRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasDeviceLabelRelation::getLabelId, id);
        return saasDeviceLabelRelationMapper.selectList(queryWrapper);
    }

    public List<SaasSpaceLabel> querySpaceLabels(String customerId) {
        LambdaQueryWrapper<SaasSpaceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceLabel::getType, 0);
        return saasSpaceLabelMapper.selectList(queryWrapper);
    }

    public List<SaasDeviceLabel> queryDeviceLabelId(Long spaceLabelId) {
        LambdaQueryWrapper<SaasDeviceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasDeviceLabel::getSpaceLabelId, spaceLabelId);
        return saasDeviceLabelMapper.selectList(queryWrapper);
    }

    public List<CustomLabelResponse> queryCustomLabels(String customerId) {
        List<CustomLabelResponse> responseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceLabel::getCustomerId, customerId);
        List<SaasSpaceLabel> spaceLabels = saasSpaceLabelMapper.selectList(queryWrapper);
        for (SaasSpaceLabel spaceLabel : spaceLabels){
            CustomLabelResponse customLabelResponse = new CustomLabelResponse();
            BeanUtils.copyProperties(spaceLabel, customLabelResponse);
            customLabelResponse.setType(LabelTypeEnum.SPACE.getCode());
            responseList.add(customLabelResponse);
        }
        LambdaQueryWrapper<SaasDeviceLabel> deviceQueryWrapper = new LambdaQueryWrapper<>();
        deviceQueryWrapper.eq(SaasDeviceLabel::getCustomerId, customerId);
        List<SaasDeviceLabel> deviceLabels = saasDeviceLabelMapper.selectList(deviceQueryWrapper);
        for (SaasDeviceLabel deviceLabel : deviceLabels){
            CustomLabelResponse customLabelResponse = new CustomLabelResponse();
            BeanUtils.copyProperties(deviceLabel, customLabelResponse);
            customLabelResponse.setType(LabelTypeEnum.DEVICE.getCode());
            responseList.add(customLabelResponse);
        }
        return responseList;
    }

    public List<LabelResponse> queryAll(String customerId) {
        List<LabelResponse> responseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceLabel::getType, 0);
        List<SaasSpaceLabel> spaceLabels = saasSpaceLabelMapper.selectList(queryWrapper);
        for (SaasSpaceLabel spaceLabel : spaceLabels){
            if(!StringUtils.isEmpty(spaceLabel.getCustomerId()) && !spaceLabel.getCustomerId().equals(customerId)){
                continue;
            }
            LabelResponse labelResponse = new LabelResponse();
            BeanUtils.copyProperties(spaceLabel, labelResponse);
            labelResponse.setType(LabelTypeEnum.SPACE.getCode());
            List<LabelResponse> deviceLabelList = new ArrayList<>();
            List<SaasDeviceLabel> deviceLabels = saasDeviceLabelMapper.selectList(new LambdaQueryWrapper<SaasDeviceLabel>().eq(SaasDeviceLabel::getSpaceLabelId, spaceLabel.getId()));
            for (SaasDeviceLabel deviceLabel : deviceLabels){
                if(!StringUtils.isEmpty(deviceLabel.getCustomerId()) && !deviceLabel.getCustomerId().equals(customerId)){
                    continue;
                }
                LabelResponse deviceLabelResponse = new LabelResponse();
                BeanUtils.copyProperties(deviceLabel, deviceLabelResponse);
                deviceLabelResponse.setType(LabelTypeEnum.DEVICE.getCode());
                deviceLabelList.add(deviceLabelResponse);
            }
            labelResponse.setSubLabels(deviceLabelList);
            responseList.add(labelResponse);
        }
        LabelResponse customLabelResponse = new LabelResponse();
        customLabelResponse.setType(LabelTypeEnum.CUSTOM.getCode());
        customLabelResponse.setLabelName("自定义标签");
        List<CustomLabelResponse> customLabels = queryCustomLabels(customerId);
        List<LabelResponse> subLabels = new ArrayList<>();
        BeanUtils.copyProperties(customLabels, subLabels);
        customLabelResponse.setSubLabels(subLabels);
        responseList.add(customLabelResponse);
        return responseList;
    }
}
